<template>
	<view>
		<view class="w-100" v-if="list.length>0" v-for="(item,index) in list"  :key="index">
			<media-list  type="one" :item="item" :index="index"></media-list>
		</view>
		<view @click="initSearch" v-if="list.length>0" class="flex w-100 align-center justify-center text-secondary" style="height: 100rpx;">
			清除搜索
		</view>
		<!-- 默认样式 -->
		<!-- <view v-if="!keyword" class="flex w-100 align-center justify-center text-secondary" style="height: 100rpx;">
			暂无数据
		</view> -->
		<view v-if="list.length>=10" class="flex align-center justify-center py-3 bg-light">
			<text class="text-muted font">{{loadtext}}</text>
		</view>
	</view>
</template>

<script>
	import mediaList from '@/components/common/media-list.vue';

	import {mapState} from 'vuex';
	export default {
		components: {
			mediaList,
		},
		computed:{
			...mapState({
				user_id:state=>{
					return state.user?state.user.id:0
				}
			})
		},
		data() {
			return {
				list:[],
				page:1,
				loadtext:'上拉加载更多',
				keyword:''
			}
		},
		onNavigationBarSearchInputChanged(e) {
			this.keyword=e.text
		},
		onNavigationBarButtonTap(){
			if(this.keyword===''){
				return uni.showToast({
					title: '请输入关键字',
					icon: 'none'
				});
			}
			this.getData()
		},
		onPullDownRefresh() {
			this.initSearch()
		},
		onReachBottom() {
			if(this.loadtext!=='上拉加载更多'){
				return
			}
			this.page+=1
			this.getData().catch(err=>{
				this.page-=1
				this.loadtext="上拉加载更多"
			})
		},
		methods: {
			getData(){
				this.loadtext='搜索中...'
				return this.$H.get(`/search/${this.page}?keyword=${this.keyword}`).then(res=>{
					this.list=this.page===1?res:[...this.list,...res],
					this.loadtext=this.page*10===this.list.length?'上拉加载更多':'没有更多啦'
				})
			},
			initSearch(){
				this.keyword=''
				this.list=[]
			}
		}
	}
</script>

<style>

</style>
